$(function () {

    $("body").on("click", "#saveConfig", function () {
        saveAgentConfig();
        }).on("ifChecked","input[name='settType']",function (e) {
       var  settType =$(this).val();
       var sett  ="";
       switch (settType) {
           case "1" :
               sett="随时结";
               break
               ;
           case "2" :
               sett="月结";
               break
               ;
           case "3" :
               sett="年结";
               break
               ;
       }


        parent.layer.open({
            type: 2,
            shadeClose: false,
            area:winArea,
            skin: 'layui-layer-demo',
            title: '选择推广员',
            content: baseloaction+"/admin/agent/user/openSelectAgentUserList",
            btn: ['确认', '取消'],
            yes: function (index, layero) {
                var arr=$(layero).find('iframe')[0].contentWindow.agentIds();
                if(arr==""){
                    parent.layer.confirm(
                        "<div style='height:40px;font-size: 16px;color:#ed5565;'><p class='text-center'>没有选中任何推广员，默认将平台中所有正式推广员的结算周期修改为"+sett+"吗？</p></div>",
                        {
                            type: 1,
                            title:'友情提示：',
                            area: ['420px'],
                            offset: 'auto',
                            closeBtn:0,
                            btn:['确认',"取消"],
                            btnAlign : 'c',
                            shade: [0.3, '#000000']
                        },
                        function (index) {
                            alterAgentSettType(settType,index,arr);
                        },
                        function () {
                            parent.layer.closeAll();
                        });
                }else{
                    alterAgentSettType(settType,index,arr);
                    parent.layer.closeAll();
                }
            }
        });

    });

    function saveAgentConfig() {
        var params = $("#agentConfigForm").serializeJSON();
        delete  params.settType;
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + "/admin/agent/config/saveAgentConfig",
            data: params,
            type: "post",
            dataType:"json",
            contentType:'application/json;charset=utf-8',
            success: function (result) {
                icModale(0, result.message);
            },
            error: function (error) {
                icModale(0,"系统繁忙，保存失败");
            }
        })
    }


    function alterAgentSettType(settType,index,arr) {

        $.ajax({
            url: baseloaction + '/admin/agent/user/alterAgentSettType',
            type:'post',
            dataType:'json',
            data:{
                'settType':settType,
                "ids":arr
            },
            success:function (result) {
                parent.layer.closeAll();
                icModale(0, result.message);
            },
            error: function (error) {
                icModale(0,"系统繁忙，保存失败");
            }
        });
    }
    initSummerNote($("#depict"))

    function initSummerNote(target) {
        target.summernote({
            height: 120,
            toolbar: [
                ['style', ['bold', 'italic', 'underline', 'clear']],
                ['font', ['strikethrough', 'superscript', 'subscript']],
                ['fontsize', ['fontsize']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']]
            ],
            lang: 'zh-CN',
            buttons: {},
            callbacks: {}
        });
    }
})